from keras.engine.input_layer import Input
import numpy
from keras import backend as K
from keras.layers import Dense, Flatten,Dropout,RNN,InputLayer,LSTM,Reshape
from keras.layers.convolutional import Conv2D,MaxPooling2D
from keras.models import Sequential
from keras.utils.vis_utils import plot_model
from keras.layers import Flatten
from keras.layers.core import Activation
from keras.datasets import mnist
import keras 
import tensorflow
import matplotlib.pyplot as plt
import numpy 

num_classes = 10
batch_size = 32
epochs = 10 
img_row, img_col = 28, 28
(X_train,y_train),(X_test,y_test) = mnist.load_data()


plt.subplot(221)
plt.imshow(X_train[0],cmap=plt.get_cmap('gray'))
plt.subplot(222)
plt.imshow(X_train[1],cmap=plt.get_cmap('gray'))
plt.subplot(223)
plt.imshow(X_train[2],cmap=plt.get_cmap('gray'))
plt.subplot(224)
plt.imshow(X_train[3],cmap=plt.get_cmap('gray'))
plt.show()

numpy.random.seed(0)

# 归一化处理
X_train = X_train / 255
X_test = X_test / 255

# 数据探查
print("number of training examples = %i" % X_train.shape[0])
print("Number of classes = %i" % len(numpy.unique(y_train)))
unique, count = numpy.unique(y_train,return_counts=True)
print("the number of occrrences of each class is %s" % dict(zip(unique,count)))

# 数据处理
X_train = X_train.reshape(X_train.shape[0],28,28,1).astype('float32') 
X_test = X_test.reshape(X_test.shape[0],28,28,1).astype('float32') 
y_train = tensorflow.keras.utils.to_categorical(y_train, num_classes)
y_test = tensorflow.keras.utils.to_categorical(y_test, num_classes)

model = Sequential()
model.add(Input(shape=(28,28,1)))
model.add(Flatten())
model.add(Reshape((1,784)))
model.add(LSTM(32, return_sequences=False, stateful=False,))
model.add(Reshape((1,32)))
model.add(LSTM(32, return_sequences=False, stateful=False,))
model.add(Dense(16,activation="relu"))
model.add(Dense(10))
model.add(Activation("softmax"))

print(model.summary())
plot_model(model,to_file='shared_input_layer.png')
opt = keras.optimizers.RMSprop(learning_rate=0.0001,decay = 1e-6)
model.compile(loss='categorical_crossentropy',optimizer=opt,metrics=['accuracy'])
model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs,validation_data=(X_test,y_test),shuffle=True)
scores = model.evaluate(X_test,y_test,verbose=1)
print("Test loss:",scores[0])
print("Test accuracy:",scores[1])

